<?php
class admin_listController extends adminController{

    //首页
    public function index() {
        $this->app_list=model('app_manage')->app_list();
        $this->show();  
    }

    //安装
    public function install() {
        $name=in($_POST['name']);
        $this->alert_str($name,'',true);
        //获取APP信息
        $info=appConfig($name);
        if($info['APP_INSTALL']==1){
        	$this->msg('该APP已经安装过了！',0);
        }
        if($info['APP_RELY']){
        	$rely=explode(',', $info['rely']);
        	if(!empty($rely)){
        		foreach ($rely as $value) {
        			if(!appConfig($value)){
        				$this->msg('没有发现依赖APP：'.$value,0);
        			}
        		}
        	}
        }
        $app_base=BASE_PATH . 'apps/' . $name . '/';
        $db_dir = $app_base.'data/';
        if(is_dir($db_dir)){
	        //导入数据库
	        $config=config('DB');
	        $db = new Dbbak($config['DB_HOST'],$config['DB_USER'],$config['DB_PWD'],$config['DB_NAME'],'utf8',$db_dir);
	        $db->importSql('',$info['APP_ORIGINAL_PREFIX'],$config['DB_PREFIX']);
        }

        //移动文件
        $public_dir=$app_base.'public/';
        if(is_dir($public_dir)){
        copy_dir($public_dir, ROOT_PATH . 'public/' . $name);
        }

        //写入配置
        $data=array();
        $data['APP_STATE']=1;
        $data['APP_INSTALL']=1;
		if( save_config($name, $data) ){
            cacheApi();
			$this->msg('安装成功！');
		}	
		$this->msg('安装失败！可能由于您的目录没有读写权限！',0);
    }

    //卸载
    public function uninstall() {
    	$name=in($_POST['name']);
        $this->alert_str($name,'',true);
        //获取APP信息
        $info=appConfig($name);
        if(!$info['APP_INSTALL']){
        	$this->msg('该APP已经卸载或者未安装！',0);
        }
        //卸载数据库
        if(!empty($info['APP_TABLES'])){
        	model('app_manage')->uninstall($info['APP_TABLES']);
        }

        //删除资源文件
        $public_dir=ROOT_PATH . 'public/' . $name;
		if( is_dir( $public_dir ) ){
			del_dir( $public_dir );
		}
		//删除app文件
		del_dir(BASE_PATH . 'apps/' . $name);
        cacheApi();
		$this->msg('卸载成功！');
    }


    //导出
    public function export() {
    	$name=in($_POST['name']);
        $this->alert_str($name,'',true);
        //获取APP信息
        $info=appConfig($name);
        if(!$info['APP_INSTALL']){
        	$this->msg('该APP已经卸载或者未安装！',0);
        }
        //导出数据库
        $app_base=BASE_PATH . 'apps/' . $name . '/';
        $db_dir = $app_base.'data/';
        if(!empty($info['APP_TABLES'])){
        	$config=config('DB');
        	$db = new Dbbak($config['DB_HOST'],$config['DB_USER'],$config['DB_PWD'],$config['DB_NAME'],'utf8',$db_dir);
        	$tables = explode(',', $info['APP_TABLES']);
        	foreach ($tables as $table) {
        		$table_array[]=$config['DB_PREFIX'].$table;
        	}
	        if(!$db->exportSql($table_array)){
	            $this->msg('数据库导出失败！',0);
	        }
	    }
	    //移动文件
        $public_dir=ROOT_PATH . 'public/';
        if(is_dir($public_dir)){
        	copy_dir($public_dir . $name , $app_base.'public/' );
        }
		$this->msg('导出成功，请自行下载APP目录下的APP文件！');
    }

    //停用启用
    public function status() {
    	$name=in($_POST['name']);
        $this->alert_str($name,'',true);
        //获取APP信息
        $info=appConfig($name);
        if(!$info['APP_STATE']){
        	$status=1;
        }else{
        	$status=0;
        }
        //写入配置
        $data=array();
        $data['APP_STATE']=$status;
		if( save_config($name, $data) ){
			$this->msg('状态更改成功！');
		}	
		$this->msg('状态更改失败！可能由于您的目录没有读写权限！',0);

    }




}